package jmu.cze.controller;

import jmu.cze.service.PersonService;
import jmu.cze.until.JsonUtil;
import jmu.cze.vo.Person;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;

@Controller
@RequestMapping("/user")
public class UserController {
    private PersonService personService;

    @Resource(name = "personServiceImpl")
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    @GetMapping("/login")
    public String loginPage() {
        return "user/login";
    }

    @PostMapping("/login")
    @ResponseBody
    public String login(@RequestParam("id") String id, @RequestParam("password") String password,@RequestParam("type") int type, HttpSession session) {
        Person person = personService.signIn(id, password,type);
        if (person != null) {
            session.setAttribute("user", person.getName());
            session.setAttribute("id", person.getId());
            session.setAttribute("type",person.getType());
            return JsonUtil.success("登录成功");
        }
        return JsonUtil.failure("账号或密码错误");
    }

    @GetMapping("modify/")
    public String modifyPage(Model model, HttpSession session) {
        Person person = personService.findByID((String) session.getAttribute("id"));
        if (person == null)
            return "redirect:/user/login";
        model.addAttribute("person", person);
        return "user/modify";
    }

    @PostMapping("/modify")
    @ResponseBody
    public String modify(Person person, HttpSession session) {
        person.setId((String) session.getAttribute("id"));
        person.setType((Integer) session.getAttribute("type"));
        if (personService.modify(person)) {
            session.setAttribute("user", person.getName());
            return JsonUtil.success("修改成功");
        }
        return JsonUtil.failure("修改失败");
    }

    @PostMapping("/register")
    @ResponseBody
    public String register(Person person) {
        person.setType(0);
        try {
            if (personService.signUp(person)) {
                return JsonUtil.success("注册成功");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }catch (DuplicateKeyException e){
            e.printStackTrace();
        }
        return JsonUtil.failure("注册失败");
    }

    @PostMapping("/destroy")
    @ResponseBody
    public String destroy(String id, HttpSession session) {
        if (personService.destroy(id)) {
            session.invalidate();
            return JsonUtil.success("删除成功");
        }
        return JsonUtil.failure("删除失败");
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/";
    }
}